{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 解读 {func}`~tvm.target.override_native_generic_func`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/media/pc/data/lxw/ai/tvm-book/doc/read\n"
     ]
    }
   ],
   "source": [
    "%cd ..\n",
    "import set_env"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tvm\n",
    "@tvm.target.override_native_generic_func(\"test_target_temp_strategy\")\n",
    "def target_generic(data):\n",
    "    # default generic function\n",
    "    return data + 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "@target_generic.register([\"cuda\", \"gpu\"])\n",
    "def target_cuda_func(data):\n",
    "    return data + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def temp_target_cuda_func(data):\n",
    "    return data + 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[20:11:25] /media/pc/data/lxw/ai/tvm/src/target/target_kind.cc:158: Warning: Unable to detect CUDA version, default to \"-arch=sm_50\" instead\n",
      "[20:11:25] /media/pc/data/lxw/ai/tvm/src/target/target_kind.cc:158: Warning: Unable to detect CUDA version, default to \"-arch=sm_50\" instead\n",
      "[20:11:25] /media/pc/data/lxw/ai/tvm/src/target/target_kind.cc:158: Warning: Unable to detect CUDA version, default to \"-arch=sm_50\" instead\n",
      "[20:11:25] /media/pc/data/lxw/ai/tvm/src/target/target_kind.cc:158: Warning: Unable to detect CUDA version, default to \"-arch=sm_50\" instead\n",
      "[20:11:25] /media/pc/data/lxw/ai/tvm/src/target/target_kind.cc:158: Warning: Unable to detect CUDA version, default to \"-arch=sm_50\" instead\n"
     ]
    }
   ],
   "source": [
    "class TempStrategy:\n",
    "    def __init__(self, name, target, fstrategy):\n",
    "        generic_fstrategy = tvm.target.get_native_generic_func(name)\n",
    "        self.target = target\n",
    "        self.name = name\n",
    "        self.origin_func = {}\n",
    "        with tvm.target.Target(target) as target_obj:\n",
    "            for tgt_key in target_obj.keys:\n",
    "                self.origin_func[tgt_key] = generic_fstrategy.get_packed_func()\n",
    "                generic_fstrategy.register(fstrategy, tgt_key, allow_override=True)\n",
    "\n",
    "    def __enter__(self):\n",
    "        return self\n",
    "\n",
    "    def __exit__(self, typ, value, traceback):\n",
    "        generic_fstrategy = tvm.target.get_native_generic_func(self.name)\n",
    "        with tvm.target.Target(self.target) as target_obj:\n",
    "            for tgt_key in target_obj.keys:\n",
    "                generic_fstrategy.register(\n",
    "                    self.origin_func[tgt_key], tgt_key, allow_override=True\n",
    "                )\n",
    "\n",
    "with tvm.target.Target(\"cuda\"):\n",
    "    assert target_generic(1) == 3\n",
    "\n",
    "# The strategy func change to temp_target_cuda_func.\n",
    "with TempStrategy(\"test_target_temp_strategy\", \"cuda\", temp_target_cuda_func):\n",
    "    with tvm.target.Target(\"cuda\"):\n",
    "        assert target_generic(1) == 4\n",
    "\n",
    "with tvm.target.Target(\"cuda\"):\n",
    "    assert target_generic(1) == 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py312x",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
